package com.sevattal.service.leaves.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sevattal.common.base.result.ResultCodeEnum;
import com.sevattal.service.base.exception.SevattalException;
import com.sevattal.service.leaves.entity.PurchaseArrivalChildOrder;
import com.sevattal.service.leaves.entity.PurchaseArrivalMainOrder;
import com.sevattal.service.leaves.entity.vo.PurchaseArrivalChildOrderVo;
import com.sevattal.service.leaves.entity.vo.PurchaseArrivalMainOrderVo;
import com.sevattal.service.leaves.entity.vo.PurchaseArrivalOrderVo;
import com.sevattal.service.leaves.entity.vo.PurchaseOrderVo;
import com.sevattal.service.leaves.mapper.PurchaseArrivalChildOrderMapper;
import com.sevattal.service.leaves.mapper.PurchaseArrivalMainOrderMapper;
import com.sevattal.service.leaves.service.PurchaseArrivalOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  采购到货主表 服务实现类
 * </p>
 *
 * @author Sevattal
 * @since 2021-08-28
 */
@Service
public class PurchaseArrivalOrderServiceImpl implements PurchaseArrivalOrderService {

    @Autowired
    private PurchaseArrivalMainOrderMapper mainOrderMapper;
    @Autowired
    private PurchaseArrivalChildOrderMapper childOrderMapper;


    @Override
    public boolean createPurchaseArrivalOrder(PurchaseArrivalOrderVo orderVo) {
        JudgmentData(orderVo);
        QueryWrapper<PurchaseArrivalMainOrder> mainOrderQueryWrapper = new QueryWrapper<>();
        mainOrderQueryWrapper.eq("f_pamId",orderVo.getFPamid());
        int count = mainOrderMapper.selectCount(mainOrderQueryWrapper);
        if (count > 0){
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        mainOrderMapper.insert(getPurchaseArrivalMainOrder(orderVo));
        List<PurchaseArrivalChildOrder> childOrders = getPurchaseArrivalChildOrder(orderVo);
        for (int i =0;i<childOrders.size();i++){
            childOrderMapper.insert(childOrders.get(i));
        }
        return true;
    }

    @Override
    public boolean updatePurchaseArrivalOrder(PurchaseArrivalOrderVo orderVo) {
        JudgmentData(orderVo);
        QueryWrapper<PurchaseArrivalMainOrder> mainOrderQueryWrapper = new QueryWrapper<>();
        mainOrderQueryWrapper.eq("f_pamId",orderVo.getFPamid());
        int count = mainOrderMapper.selectCount(mainOrderQueryWrapper);
        if (count == 0){
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        mainOrderMapper.update(getPurchaseArrivalMainOrder(orderVo),mainOrderQueryWrapper);

        QueryWrapper<PurchaseArrivalChildOrder> childOrderQueryWrapper = new QueryWrapper<>();
        childOrderQueryWrapper.eq("f_pamId",orderVo.getFPamid());
        childOrderMapper.delete(childOrderQueryWrapper);
        List<PurchaseArrivalChildOrder> childOrders = getPurchaseArrivalChildOrder(orderVo);
        for (int i =0;i<childOrders.size();i++){
            childOrderMapper.insert(childOrders.get(i));
        }
        return true;
    }

    @Override
    public boolean deletePurchaseArrivalOrder(String id) {
        QueryWrapper<PurchaseArrivalMainOrder> mainOrderQueryWrapper = new QueryWrapper<>();
        mainOrderQueryWrapper.eq("f_pamId",id);
        mainOrderMapper.delete(mainOrderQueryWrapper);
        QueryWrapper<PurchaseArrivalChildOrder> childOrderQueryWrapper = new QueryWrapper<>();
        childOrderQueryWrapper.eq("f_pamId",id);
        childOrderMapper.delete(childOrderQueryWrapper);
        return true;
    }

    @Override
    public List<PurchaseArrivalOrderVo> getPurchaseArrivalOrderList(PurchaseArrivalOrderVo orderVo) {
        QueryWrapper<PurchaseArrivalMainOrder> mainOrderQueryWrapper = new QueryWrapper<>();
        mainOrderQueryWrapper.orderByAsc("pam.f_pamId");
        mainOrderQueryWrapper.eq("pam.is_disabled",0);
        mainOrderQueryWrapper.eq("pam.is_deleted",0);
        if (orderVo.getFPamid() != null && !StringUtils.isEmpty(orderVo.getFPamid())){
            mainOrderQueryWrapper.like("pam.f_pamID",orderVo.getFPamid());
        }
        if (orderVo.getFEname() != null && !StringUtils.isEmpty(orderVo.getFEname())){
            mainOrderQueryWrapper.like("e.f_eName",orderVo.getFEname());
        }
        if (orderVo.getFPname() != null && !StringUtils.isEmpty(orderVo.getFPname())){
            mainOrderQueryWrapper.like("p.f_pName",orderVo.getFPname());
        }
        if (orderVo.getFWhid() != null && !StringUtils.isEmpty(orderVo.getFWhid())){
            mainOrderQueryWrapper.eq("pam.f_whId",orderVo.getFWhid());
        }
        if (orderVo.getSearchStartDate() != null){
            mainOrderQueryWrapper.ge("pam.f_pamDate",orderVo.getSearchStartDate());
        }
        if (orderVo.getSearchEndDate() != null) {
            mainOrderQueryWrapper.le("pam.f_pamDate",orderVo.getSearchEndDate());
        }
        List<PurchaseArrivalMainOrderVo> mainOrderVos = mainOrderMapper.getPurchaseMainOrder(mainOrderQueryWrapper);

        QueryWrapper<PurchaseArrivalChildOrder> childOrderQueryWrapper = new QueryWrapper<>();
        List<PurchaseArrivalOrderVo> orderVoList = new ArrayList<>();
        for (int i =0; i<mainOrderVos.size();i++){
            childOrderQueryWrapper.eq("pac.is_disabled",0);
            childOrderQueryWrapper.eq("pac.is_deleted",0);
            childOrderQueryWrapper.eq("pac.f_pamId",mainOrderVos.get(i).getFPamid());
            List<PurchaseArrivalChildOrderVo> childOrderVos = childOrderMapper.getPurchaseChildOrder(childOrderQueryWrapper);
            orderVoList.add(getPurchaseArrivalOrderVo(mainOrderVos.get(i),childOrderVos));
            childOrderQueryWrapper.clear();
        }

        return orderVoList;
    }

    @Override
    public boolean confirmOrder(String id) {
        QueryWrapper<PurchaseArrivalMainOrder> mainOrderQueryWrapper = new QueryWrapper<>();
        mainOrderQueryWrapper.eq("f_pamId",id);
        int count = mainOrderMapper.selectCount(mainOrderQueryWrapper);
        if (count != 1) {
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        PurchaseArrivalMainOrder mainOrder = new PurchaseArrivalMainOrder();
        mainOrder.setFPamstatus("1");
        mainOrderMapper.update(mainOrder,mainOrderQueryWrapper);
        return true;
    }

    public void JudgmentData(PurchaseArrivalOrderVo orderVo){
        if (orderVo.getFPamid() == null || StringUtils.isEmpty(orderVo.getFPamid()) ||
                orderVo.getPurchaseArrivalChildOrderVoList().size() == 0 ){
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
    }

    public  PurchaseArrivalMainOrder  getPurchaseArrivalMainOrder(PurchaseArrivalOrderVo orderVo){
        PurchaseArrivalMainOrder mainOrder = new PurchaseArrivalMainOrder();
        mainOrder.setFPamid(orderVo.getFPamid());
        mainOrder.setFPamwaybill(orderVo.getFPamwaybill());
        mainOrder.setFPmid(orderVo.getFPmid());
        mainOrder.setFEid(orderVo.getFEid());
        mainOrder.setFPid(orderVo.getFPid());
        mainOrder.setFWhid(orderVo.getFWhid());
        mainOrder.setFPamdate(orderVo.getFPamdate());
        mainOrder.setFPamstatus(orderVo.getFPamstatus());
        mainOrder.setFPamconfirm(orderVo.getFPamconfirm());
        mainOrder.setFPammemo(orderVo.getFPammemo());
        return mainOrder;
    }

    public List<PurchaseArrivalChildOrder> getPurchaseArrivalChildOrder(PurchaseArrivalOrderVo orderVo){
        List<PurchaseArrivalChildOrder> childOrderList = new ArrayList<>();
        PurchaseArrivalChildOrder childOrder = new PurchaseArrivalChildOrder();
        for (int i = 0;i<orderVo.getPurchaseArrivalChildOrderVoList().size();i++){
            childOrder.setFPamid(orderVo.getFPamid());
            childOrder.setFMid(orderVo.getPurchaseArrivalChildOrderVoList().get(i).getFMid());
            childOrder.setFPacquantity(orderVo.getPurchaseArrivalChildOrderVoList().get(i).getFPacquantity());
            childOrder.setFPacdate(orderVo.getPurchaseArrivalChildOrderVoList().get(i).getFPacdate());
            childOrder.setFPacstatus(orderVo.getPurchaseArrivalChildOrderVoList().get(i).getFPacstatus());
            childOrder.setFPacmemo(orderVo.getPurchaseArrivalChildOrderVoList().get(i).getFPacmemo());
            childOrderList.add(childOrder);
        }
        return  childOrderList;
    }

    public PurchaseArrivalOrderVo getPurchaseArrivalOrderVo(PurchaseArrivalMainOrderVo mainOrderVo, List<PurchaseArrivalChildOrderVo> childOrderVos){
        PurchaseArrivalOrderVo orderVo = new PurchaseArrivalOrderVo();
        orderVo.setFPamid(mainOrderVo.getFPamid());
        orderVo.setFPamwaybill(mainOrderVo.getFPamwaybill());
        orderVo.setFPmid(mainOrderVo.getFPmid());
        orderVo.setFEid(mainOrderVo.getFEid());
        orderVo.setFEname(mainOrderVo.getFEname());
        orderVo.setFPid(mainOrderVo.getFPid());
        orderVo.setFPname(mainOrderVo.getFPname());
        orderVo.setFWhid(mainOrderVo.getFWhid());
        orderVo.setFWhname(mainOrderVo.getFWhname());
        orderVo.setFPamdate(mainOrderVo.getFPamdate());
        orderVo.setFPamstatus(mainOrderVo.getFPamstatus());
        orderVo.setFPamconfirm(mainOrderVo.getFPamconfirm());
        orderVo.setFPammemo(mainOrderVo.getFPammemo());
        orderVo.setPurchaseArrivalChildOrderVoList(childOrderVos);
        return orderVo;
    }

}
